//
//  CacheManager.m
//  TestCache
//
//  Created by simon on 10-9-11.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CacheManager.h"


@implementation CacheManager



//取得缓存的目录地址
+(NSString*)getCacheDirectory{
	NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	return cachePath;
}

//清理缓存
+(BOOL)clearCache{
	NSFileManager* fileManager = [NSFileManager defaultManager];
	NSError* error = nil;
	BOOL success = [fileManager removeItemAtPath:[self getCacheDirectory] error:&error];
	return success;
}

//从缓存中取出某个图片
+(UIImage*)getImageAccordingToURLString:(NSString*)imageURLString{
    
	imageURLString = [imageURLString stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
	
	NSFileManager* fileManager = [NSFileManager defaultManager];
	NSString* imageURL = [[self getCacheDirectory] stringByAppendingFormat:@"/%@", imageURLString];
	NSData* data = [fileManager contentsAtPath:imageURL];
	if(data == nil){
		//缓存中没取到,到bundle里面取
		NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
		data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath, imageURLString]];
	}
	UIImage* image = [[UIImage alloc] initWithData:data];
	return [image autorelease];
}


//往缓存里面放图片
+(BOOL)setImage:(UIImage*)image withImageURLString:(NSString*)imageURLString{
    
	imageURLString = [imageURLString stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
	
	if(imageURLString == nil){
		return NO;
	}
	
	NSFileManager* fileManager = [NSFileManager defaultManager];
	NSData* data = nil;
	
	if([imageURLString hasSuffix:@"jpg"]){
		data = UIImageJPEGRepresentation(image, 1.0);
	}else if([imageURLString hasSuffix:@"png"]){
		data = UIImagePNGRepresentation(image);
	}
	else {
		data = UIImageJPEGRepresentation(image, 1.0);
	}

	
	NSString* imageURL = [[self getCacheDirectory] stringByAppendingFormat:@"/%@", imageURLString];
	BOOL success = [fileManager createFileAtPath:imageURL contents:data attributes:nil];
	return success;
}

//往缓存里面放xml
+(BOOL)setXML:(NSString*)xmlString withFileName:(NSString*)xmlFileName{
	
	NSFileManager* fileManager = [NSFileManager defaultManager];
	NSString* filePath = nil;
	if ([xmlFileName hasSuffix:@"xml"]) {
		filePath = [NSString stringWithFormat:@"%@/%@",[self getCacheDirectory],xmlFileName];
	}else {
		filePath = [NSString stringWithFormat:@"%@/%@.xml",[self getCacheDirectory],xmlFileName];
	}

	
	if(xmlString == nil){ 
		//如果xml是空的,返回NO
		return NO;
	}

	NSData* data = [xmlString dataUsingEncoding:NSUTF8StringEncoding];
	BOOL success = [fileManager createFileAtPath:filePath contents:data attributes:nil];
	return success;	
}

//获取到bundle里面的数据
+(NSString*)getXMLStringFromBundleWithFileName:(NSString*)xmlFileName{	

	NSError* error;
	NSString* xmlString = nil;
	if(xmlFileName != nil){ 
		//到bundle里面取
		NSString* bundlePath = [[NSBundle mainBundle] pathForResource:xmlFileName ofType:@"xml"];		
		xmlString = [NSString stringWithContentsOfFile:bundlePath encoding:NSUTF8StringEncoding error:&error];
	}
	return xmlString;
}

//获取到缓存中的数据
+(NSString*)getXMLStringWithFileName:(NSString*)xmlFileName{
	NSString* filePath = nil;
	NSError* error;
	
	if ([xmlFileName hasSuffix:@"xml"]) {
		filePath = [NSString stringWithFormat:@"%@/%@",[self getCacheDirectory],xmlFileName];
	}else {
		filePath = [NSString stringWithFormat:@"%@/%@.xml",[self getCacheDirectory],xmlFileName];
	}

	//从缓存中取数据
	NSString* xmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
	if(xmlString == nil){ 
		//如果是空的,到bundle里面取
		NSString* bundlePath = [[NSBundle mainBundle] pathForResource:xmlFileName ofType:@"xml"];		
		xmlString = [NSString stringWithContentsOfFile:bundlePath encoding:NSUTF8StringEncoding error:&error];
	}
	return xmlString;
}

@end
